<template>
    <div>

        <!--tabber区域-->
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item', item.id === 0 ? ' mui-active' : '']" v-for="item in cates" :key="item.id" @click="getPhotoListByCateId(item.id)">
                        {{ item.title }}
                    </a>
                </div>
            </div>
        </div>

        <!--lazy img 区域-->
        <ul class="photo-list">
            <router-link v-for="item in imgList" :key="item.id" :to="'/home/photoinfo/'+ item.id" tag="li">
                <img v-lazy="item.img_url" alt="">
                <div class="info">
                    <div class="info-title">{{ item.title }}</div>
                    <div class="info-zhaiyao">{{ item.zhaiyao }}</div>
                </div>
            </router-link>
        </ul>
    </div>
</template>

<script>
    // 1.导入 mui 的js文件
    import mui from '../../assets/mui/js/mui.js'
    import { Toast } from 'mint-ui'


    export default {
        name: "PhotoList",
        data(){
            return {
                cates:[],
                imgList:[]
            }
        },
        created(){
            this.getAllCategory();
            this.getPhotoListByCateId(0);
        },
        mounted() {
            // 当组件中的DOM结构被渲染好并放到页面中后，会执行这个钩子函数
            // 如果要操作元素了，最好在mounted里面，因为这个时候的DOM元素是最新的
            //初始化滑动控件
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        methods:{
            getAllCategory(){
                //获取所有的分类
                this.$axios.get('/api/getimgcategory').then( result => {
                    // debugger;
                    if( result.data.status === 0 ){
                        result.data.message.unshift({ id:0, title:'全部' });
                        this.cates = result.data.message;
                    }else {
                        Toast('请求失败！')
                    }
                });
            },
            getPhotoListByCateId(cateId){
                // 根据 分类Id 获取图片列表
                this.$axios.get('/api/getimages/'+ cateId).then(result=>{
                    if(result.data.status === 0){
                        this.imgList = result.data.message;
                    }
                })
            }
        }


    }
</script>

<style lang="scss" scoped>
* {
    touch-action: pan-y;
}
.mui-slider {
    background-color: #fff;
    border-bottom: 1px solid #ccc;
}
.photo-list {
    padding: 10px;
    padding-bottom: 0;
    li {
        position: relative;
        margin-bottom: 10px;
        background-color: #fff;
        text-align: center;
        box-shadow: 0 0 9px #8f8f94;
        img {
            width: 100%;
            vertical-align: middle;
        }
        img[lazy=loading] {
            width: 40px;
            height: 300px;
            margin: auto;
        }
        .info {
            position: absolute;
            overflow: hidden;
            bottom: 0;
            max-height: 95px;
            padding: 5px;
            text-align: left;
            background: rgba(0,0,0,.4);
            color: #fff;
            .info-title{
                font-size: 14px;
            }
            .info-zhaiyao{
                font-size: 12px;
            }
        }
    }
}

</style>
